package com.yang.web.featuretest.service.impl;

import com.auth0.jwt.interfaces.Claim;
import com.yang.web.featuretest.constants.Constants;
import com.yang.web.featuretest.service.IAuthService;
import com.yang.web.featuretest.util.StoreUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/9/1 13:26
 */

@Service
public class AuthService implements IAuthService {


    private boolean authenticationAction(String token) {
        if (!StoreUtil.storeToken.contains(token)) {
            // token已过期，token异常
            return false;
        }

        String userId;
        if ((userId = StoreUtil.tokenMapUser.get(token)) == null) {
           // log.info("用户id为： + {} 的用户已登录 ",userId);
            return false;
        }
        return true;
    }

    @Override
    public boolean authentication(Map<String, Claim> claims) {
        String user;
        System.out.println();
        Claim claim = claims.get("user");
        System.out.println(claim);
        if ((user = claims.get("user").asString()) == null) {
            return false;
        }

        return authenticationAction(user);
    }
}
